//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.hafu.dao;

import com.hafu.model.SpInfo;
import com.hafu.util.DbUtil;
import com.hafu.util.StringUtil;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class SpInfoDao {
    public SpInfoDao() {
    }

    public Boolean addSpInfo(SpInfo sp) {
        String sql = "insert into spinfo(name,price) values(?,?)";
        Connection conn = DbUtil.getConn();
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, sp.getName());
            preparedStatement.setBigDecimal(2, sp.getPrice());
            int i = preparedStatement.executeUpdate();
            Boolean var6;
            if (i <= 0) {
                var6 = false;
                return var6;
            }

            System.out.println("添加成功");
            var6 = true;
            return var6;
        } catch (SQLException var10) {
            var10.printStackTrace();
        } finally {
            DbUtil.closeConn();
        }

        return false;
    }

    public Boolean deleteSpInfo(int id) {
        String sql = "delete from spinfo where id=?";
        Connection conn = DbUtil.getConn();

        Boolean var6;
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                System.out.println("删除成功");
                var6 = true;
                return var6;
            }

            var6 = false;
        } catch (SQLException var10) {
            var10.printStackTrace();
            return false;
        } finally {
            DbUtil.closeConn();
        }

        return var6;
    }

    public Boolean updateSpInfo(SpInfo sp) {
        String sql = "update spinfo set name=?,price=? where id=?";
        Connection conn = DbUtil.getConn();

        Boolean var6;
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, sp.getName());
            preparedStatement.setBigDecimal(2, sp.getPrice());
            preparedStatement.setInt(3, sp.getId());
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                System.out.println("修改成功");
                var6 = true;
                return var6;
            }

            var6 = false;
        } catch (SQLException var10) {
            var10.printStackTrace();
            return false;
        } finally {
            DbUtil.closeConn();
        }

        return var6;
    }

    public ArrayList<SpInfo> getSpInfoByName(SpInfo sp) {
        String sql = "select * from spinfo";
        if (!StringUtil.isEmpty(sp.getName())) {
            sql = sql + " where name like '%" + sp.getName() + "%'";
        }

        Connection conn = DbUtil.getConn();
        ArrayList spList = new ArrayList();

        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();

            while(resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                BigDecimal price = resultSet.getBigDecimal("price");
                SpInfo spInfo = new SpInfo(id, name, price);
                spList.add(spInfo);
            }
        } catch (SQLException var15) {
            var15.printStackTrace();
        } finally {
            DbUtil.closeConn();
        }

        return spList;
    }
}
